﻿using System.Collections.Generic;
using System.ComponentModel;
using OxyPlot;
using OxyPlot.Axes;
using OxyPlot.Series;

namespace DesigneSpaceExplorerVEGA.Models
{
    public class VegaPlotModel : INotifyPropertyChanged
    {
        private PlotModel _plotModel;

        public PlotModel PlotModel
        {
            get { return _plotModel; }
            set
            {
                _plotModel = value;
                OnPropertyChanged("PlotModel");
            }
        }

        public VegaPlotModel()
        {
            PlotModel = new PlotModel();
            Title = "Graf performanță și consum";
            Points = new List<ScatterPoint>();
            SetUpModel();
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }

        public string Title { get; set; }

        public IList<ScatterPoint> Points { get; set; }

        private void SetUpModel()
        {
            var axisY = new LinearAxis();
            axisY.MajorGridlineStyle = LineStyle.Solid;
            axisY.MinorGridlineStyle = LineStyle.Dot;
            axisY.Title = "Consum";
            PlotModel.Axes.Add(axisY);
            var axisX = new LinearAxis();
            axisX.MajorGridlineStyle = LineStyle.Solid;
            axisX.MinorGridlineStyle = LineStyle.Dot;
            axisX.Position = AxisPosition.Bottom;
            axisX.Title = "Performanță";
            PlotModel.Axes.Add(axisX);

            LoadData();
        }

        private void LoadData()
        {
          /*  var lineSerie = new ScatterSeries();


            lineSerie.Points.Add(new ScatterPoint(0, 0, 0));
            PlotModel.Series.Add(lineSerie);*/
        }

        public void UpdateModel(List<ScatterPoint> points)
        {
            var lineSerie = new ScatterSeries()
            {
                MarkerSize = 3,
                MarkerFill = OxyColors.Red,
                /* OxyColor.FromRgb((byte) RandomSingleton.Rand.Next(255), (byte) RandomSingleton.Rand.Next(255),
                        (byte) RandomSingleton.Rand.Next(255)),*/
            };

            foreach (var scatterPoint in points)
            {
                lineSerie.Points.Add(scatterPoint);
            }

            foreach (var series in PlotModel.Series)
            {
                var scatterSeries = (ScatterSeries) series;
                scatterSeries.MarkerFill = OxyColors.Blue;
            }
            PlotModel.Series.Add(lineSerie);
        }
    }
}